cmake_minimum_required(VERSION 3.8)
project(cpp-concurrency)

#set(CMAKE_CXX_COMPILER "g++-9")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ./bin)

add_executable(hello_thread               01_hello_thread.cpp)
add_executable(lambda_thread              02_lambda_thread.cpp)
add_executable(thread                     02_thread.cpp)
add_executable(thread_argument            03_thread_argument.cpp)
add_executable(thread_self_manage         04_thread_self_manage.cpp)
add_executable(call_once                  05_call_once.cpp)
add_executable(naive_multithread          06_naive_multithread.cpp)
add_executable(mutex_lock                 07_mutex_lock.cpp)
add_executable(improved_mutex_lock        08_improved_mutex_lock.cpp)
add_executable(deadlock_bank_transfer     09_deadlock_bank_transfer.cpp)
add_executable(improved_bank_transfer     10_improved_bank_transfer.cpp)
add_executable(bank_transfer_wait_notify  11_bank_transfer_wait_notify.cpp)
add_executable(async_task                 12_async_task.cpp)
add_executable(packaged_task              13_packaged_task.cpp)
add_executable(promise_future             14_promise_future.cpp)

# For OS X
if(APPLE)
  if (DEFINED ENV{tbb_path})
    message("tbb_path=" $ENV{tbb_path})
    include_directories($ENV{tbb_path}/include/)
    link_directories($ENV{tbb_path}/lib/)
    add_executable(parallel_algorithm 15_parallel_algorithm.cpp)
    target_link_libraries(parallel_algorithm tbb)
  else()
    message("[Warning] 'tbb_path' not defined, 'export tbb_path=xxx' to build parallel algorithm.")
  endif()
endif()

# For Linux
IF("${CMAKE_SYSTEM}" MATCHES "Linux")
  add_executable(parallel_algorithm                 15_parallel_algorithm.cpp)
  target_link_libraries(parallel_algorithm          tbb)
  target_link_libraries(hello_thread                pthread)
  target_link_libraries(lambda_thread               pthread)
  target_link_libraries(thread_argument             pthread)
  target_link_libraries(thread_self_manage          pthread)
  target_link_libraries(call_once                   pthread)
  target_link_libraries(naive_multithread           pthread)
  target_link_libraries(mutex_lock                  pthread)
  target_link_libraries(improved_mutex_lock         pthread)
  target_link_libraries(deadlock_bank_transfer      pthread)
  target_link_libraries(improved_bank_transfer      pthread)
  target_link_libraries(bank_transfer_wait_notify   pthread)
  target_link_libraries(async_task                  pthread)
  target_link_libraries(packaged_task               pthread)
  target_link_libraries(promise_future              pthread)
endif()

